/*
 SGRecyclableItemView.m
 Date Created: March 6, 2010
 
 Authors: Karanvir Gill <viper1029@gmail.com>
          Frank Lau <flaua@sfu.ca>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "SGRecyclableItemView.h"


@implementation SGRecyclableItemView
@synthesize isBeingDragged;
@synthesize hasBeenDropped;
@synthesize item;

- (SGRecyclableItemView *)initWithItem:(SGRecyclableItem *)newItem {
	self = [super init];
	if( self ) {
    item=newItem;
    [item retain];
		// Create temporary image to determine the size of the frame to create
    SGRecyclableType *itemType=item.type;
		UIImage *tempImage = [UIImage imageNamed:[[itemType image] objectAtIndex:arc4random() % itemType.image.count]];
		
    
		if(item.lifeGiver){// Add a background if item is lifegiver
			UIImage *background = [UIImage imageNamed:@"lifegiverfade.png"];
      // Size of background
      backgroundWidth  = 100;
      backgroundHeight = 100;
			tempImage = [self combineImage:tempImage toBackground:background];
		}
    else { //add a transparent background so small images are easier to drag
      UIImage *background = [UIImage imageNamed:@"SGclearBackground.png"];
      // Size of background
      backgroundWidth = 95;
      backgroundHeight = 61;

      tempImage = [self combineImage:tempImage toBackground:background];
    }

		
		self.frame=CGRectMake(item.x,item.y, tempImage.size.width, tempImage.size.height);
		[self setImage:tempImage];
		self.center = CGPointMake(item.x,item.y);
		
		// Setup attributes of this item 
		isBeingDragged = false;
		hasBeenDropped = false;
	}
	return self;
}

- (void)setCenter:(CGPoint)newCenter {
  [super setCenter:newCenter];
  item.x=newCenter.x;
  item.y=newCenter.y;
}

- (void) removeFromSuperviewWithAnimation:(SGRecyclableItemViewRemoveFromSuperviewAnimationStyle)animationStyle {
  [UIView beginAnimations:@"removeItem" context:nil];
  [UIView setAnimationDelegate:self];
  [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];  
  
  if(animationStyle==SGRecyclableItemViewRemoveFromSuperviewAnimationStyleDropDown) {
    // Simulate the dropping by making the icon shrink
    CGAffineTransform transform = CGAffineTransformMakeScale(0.01, 0.01);
    self.transform = transform;
    [UIView setAnimationDuration:1.5]; // Slow it down
  } else if(animationStyle==SGRecyclableItemViewRemoveFromSuperviewAnimationStyleDissapear) {
    self.alpha=0.0;
  }
  
  [UIView commitAnimations];
  
  [item.managedObjectContext deleteObject:item];
  [item release];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
  [self removeFromSuperview];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
  // Perform the move
	CGPoint currentLocation = [[touches anyObject] locationInView:self];
	CGRect frame = [self frame];
	
	frame.origin.x += currentLocation.x - startLocation.x;
	frame.origin.y += currentLocation.y - startLocation.y;
	[self setFrame:frame];

}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
	isBeingDragged = true;
	CGPoint currentLocation = [[touches anyObject] locationInView:self]; 
	startLocation = currentLocation;
	[[self superview] bringSubviewToFront:self];
  
  // Grow the item, to make it bigger to indicate that something is happening
  // There is no need to shrink the item later, because the shrinking will
  // be done by the remove animation later on.
  [UIView beginAnimations:@"growItem" context:nil];
  CGAffineTransform transform;
  if(item.lifeGiver) // Do not grow life gives by a lot, since they are already pretty huge
    transform = CGAffineTransformMakeScale(1.1, 1.1);
  else
    transform = CGAffineTransformMakeScale(1.5, 1.5);
  self.transform = transform;
  [UIView commitAnimations];
}

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
	isBeingDragged = false;
	hasBeenDropped = true;
}

- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
  isBeingDragged = false;
  hasBeenDropped = true;
}

- (UIImage*) combineImage:(UIImage*)image1 toBackground:(UIImage*)image2{
	
	UIGraphicsBeginImageContext(CGSizeMake(backgroundWidth,backgroundHeight));  
	
	// Draw the recyclabe item
	[image1 drawInRect:CGRectMake(backgroundWidth/2-image1.size.width/2, backgroundHeight/2-image1.size.height/2, image1.size.width, image1.size.height)];  
	
	// Draw the background  
	[image2 drawInRect:CGRectMake(0, 0, backgroundWidth, backgroundHeight)];
	
	UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();  
	
	UIGraphicsEndImageContext();  
	
	return combinedImage;
}

@end
